import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author 03010570
 * @date 2020/07/08
 * describe:    LeetCode : 面试题 16.11 https://leetcode-cn.com/problems/diving-board-lcci/
 */
public class LeetCode_interview_01_02 {

    public static void main(String[] args) {
        System.out.println(CheckPermutation("abc","bca"));
        System.out.println(CheckPermutation("abc","bcd"));

    }

    public static boolean CheckPermutation(String s1, String s2) {
        if(s1.length() != s2.length()){
            return false;
        }
        int[] res = new int[26];
        for (int i=0;i<s1.length();i++) {
            res[s1.charAt(i)-97]++;
            res[s2.charAt(i)-97]--;
        }
        for (int re : res) {
            if(re != 0){
                return false;
            }
        }
        return true;
    }

    public static boolean CheckPermutation2(String s1, String s2) {
        if(s1.length() != s2.length()){
            return false;
        }
        Map<Character, Integer> map1 = new HashMap<>();
        for (int i=0;i<s1.length();i++) {
            char c1 = s1.charAt(i);
            char c2 = s2.charAt(i);
            map1.put(c1, map1.getOrDefault(c1,0)+1);
            map1.put(c2, map1.getOrDefault(c2,0)-1);
        }
        for (Integer value : map1.values()) {
            if(value!=0){
                return false;
            }
        }
        return true;
    }

}
